#include "stdafx.h"
#include "VectorProperties.h"

using namespace particleEditorCli;


//vector2 ----------------------------------------------------------
float Vector2dProperties::X::get()
{
	return x;
}

void Vector2dProperties::X::set(float value)
{
	x=value;
}

float Vector2dProperties::Y::get()
{
	return y;
}
void Vector2dProperties::Y::set(float value)
{
	y = value; 
}

//vector3 ----------------------------------------------------------
float Vector3dProperties::Z::get()
{
	return z;
}

void Vector3dProperties::Z::set(float value)
{
	z = value; 
}

//vector4 ----------------------------------------------------------
float Vector4dProperties::W::get()
{
	return w;
}

void Vector4dProperties::W::set(float value)
{
	w = value; 
}

//vectors converter
bool VectorConverter::CanConvertTo(ITypeDescriptorContext^ context, System::Type^ destinationType)
{
	if (destinationType == Vector3dProperties::typeid)
		return true;
	return ExpandableObjectConverter::CanConvertTo(context,destinationType);
}

System::Object^ VectorConverter::ConvertTo(ITypeDescriptorContext^ context, CultureInfo^ culture, System::Object^ value, System::Type^ destinationType)
{
	if (destinationType == System::String::typeid)
	{
		if (value->GetType() == Vector2dProperties::typeid)
		{
			Vector2dProperties^ so = (Vector2dProperties^)value;
			return so->X + " , " + so->Y;
		}
		if (value->GetType() == Vector3dProperties::typeid)
		{
			Vector3dProperties^ so = (Vector3dProperties^)value;
			return so->X + " , " + so->Y + " , " + so->Z;
		}
		if (value->GetType() == Vector4dProperties::typeid)
		{
			Vector4dProperties^ so = (Vector4dProperties^)value;
			return so->W + " , " + so->X + " , " + so->Y + " , " + so->Z;
		}
	}
	return ExpandableObjectConverter::ConvertTo(context, culture, value, destinationType);
}

bool VectorConverter::CanConvertFrom(ITypeDescriptorContext^ context, System::Type^ sourceType)
{
	if (sourceType == System::String::typeid)
		return true;
	return ExpandableObjectConverter::CanConvertFrom(context, sourceType);
}

System::Object^ VectorConverter::ConvertFrom(ITypeDescriptorContext^ context, CultureInfo^ culture, System::Object^ value)
{
	if (value->GetType() == System::String::typeid)
	{
		try
		{
			cli::array<System::String^,1>^ data=value->ToString()->Split(',');
			switch (data->Length)
			{
			case 2:
				{
					Vector2dProperties^ v2 = gcnew Vector2dProperties();
					v2->X = float::Parse(data[0]);
					v2->Y = float::Parse(data[1]);
					return v2;
				}
			case 3:
				{
					Vector3dProperties^ v3 = gcnew Vector3dProperties();
					v3->X = float::Parse(data[0]);
					v3->Y = float::Parse(data[1]);
					v3->Z = float::Parse(data[2]);
					return v3;
				}
			case 4:
				{
					Vector4dProperties^ v4 = gcnew Vector4dProperties();
					v4->X = float::Parse(data[0]);
					v4->Y = float::Parse(data[1]);
					v4->Z = float::Parse(data[2]);
					v4->W = float::Parse(data[3]);
					return v4;
				}
			}
		}
		catch(...)
		{
			throw gcnew ArgumentException(
				"Can not convert '" + (System::String^)value +
			   "' to type VECTOR");
		}
	}
	return ExpandableObjectConverter::ConvertFrom(context, culture, value);
}